<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>需求管理</el-breadcrumb-item>
      <el-breadcrumb-item>需求分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">

        <el-col :span="4">
          <el-button type="primary" @click="addCategory">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table :data="categoryList" :columns="columns"
                  :selection-type="false" :expand-type="false"
                  show-index index-text="#" border
                  :show-row-hover="false">
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success"
             v-if="scope.row.isAlive === 1"
             style="color: lightgreen;"></i>
          <i class="el-icon-error" v-else style="color: red;"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.categoryLevel === 1">一级</el-tag>
          <el-tag type="success" size="mini" v-else>二级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button v-if="scope.row.isAlive == 1" type="primary" @click="showEditDialog(scope.row.categoryId)" icon="el-icon-edit" size="mini">编辑
          </el-button>
          <el-button v-if="scope.row.isAlive == 1" type="danger" icon="el-icon-delete" @click="deleteCategory(scope.row.categoryId)" size="mini">删除
          </el-button>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="addDialogClosed">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="addForm.categoryName"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="parentName">
          <el-select v-model="parentName" clearable placeholder="请选择" @change="parentCategorySelect">
            <el-option
              v-for="item in parentCategoryList"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.categoryId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitAddCategory()">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="修改用户信息"
      :visible.sync="editDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="editDialogClosed">
      <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="70px">
        <el-form-item label="分类名" prop="categoryName">
          <el-input v-model="editForm.categoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="changeCategory(editForm)">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
    import {addCategory, editCategory, getAllCategory, getCategoryById} from "../../assets/api/categoryApi";

    export default {
        name:'demandsCategory',
        data() {
            return {
                categoryList: [],
                pageNum: 1,
                pageSize: 5,
                total: 0,
                addDialogVisible: false,
                addForm: {categoryName: '', parentId: 0, categoryLevel: 1},
                addFormRules: {
                    categoryName: [{required: true, message: '请输入分类名称', trigger: 'blur'},
                        {min: 3, max: 10, message: '分类名称的长度在3~10个字符之间', trigger: 'blur'}],
                },
                editDialogVisible: false,
                editForm: {categoryName: ''},
                columns: [
                    {
                        label: '分类名称',
                        prop: 'categoryName'
                    },
                    {
                        label: '是否有效',
                        type: 'template',
                        template: 'isok'
                    },
                    {
                        label: '排序',
                        type: 'template',
                        template: 'order'
                    },
                    {
                        label: '操作',
                        type: 'template',
                        template: 'opt'
                    }
                ],
                parentCategoryList: [],
                parentId: 0,
                parentName: '',
            }
        },
        methods: {
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getCategoryList();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getCategoryList();
            },
            getCategoryList() {
                let data = {};
                data.categoryLevel = 0;
                getAllCategory(this.pageNum, this.pageSize, data).then((res) => {
                    if (res.code !== 200) {
                        return this.$message.error(res.message);
                    }
                    this.categoryList = res.data.records;
                    this.total = res.data.total;
                    for (let i=0;i<this.categoryList.length;i++){
                        for (let j=0;j<this.categoryList[i].children.length;j++){
                            this.total += 1;
                        }
                    }

                })
            },
            getParentCategory() {
                let data = {};
                data.categoryLevel = 1;
                getAllCategory(this.pageNum, this.pageSize, data).then((res) => {
                    if (res.code !== 200) {
                        return this.$message.error(res.message);
                    }
                    this.parentCategoryList = res.data.records;
                })
            },
            parentCategorySelect(item) {
                this.parentId = item;
                if (item === '') {
                    this.parentId = 0;
                }
            },
            addCategory() {
                this.getParentCategory();
                this.addDialogVisible = true;
            },
            addDialogClosed() {
                this.$refs.addFormRef.resetFields();
                this.parentName = '';
                this.parentId = 0;
                this.addForm.categoryLevel = 1;
                this.addForm.parentId = 0;
            },
            submitAddCategory() {
                this.$refs.addFormRef.validate(valid => {
                    if (!valid) return;
                    this.addForm.parentId = this.parentId;
                    if (this.addForm.parentId === 0) {
                        this.addForm.categoryLevel = 1;
                    } else {
                        this.addForm.categoryLevel = 2;
                    }
                    addCategory(this.addForm).then((res) => {
                        if (res.code !== 200) {
                            return this.$message.error(res.message);
                        }
                        this.$message.success(res.message);
                        this.addDialogVisible = false;
                        this.getCategoryList();
                    })
                })
            },
            showEditDialog(categoryId) {
                this.editDialogVisible = true;
                let data = {};
                data.categoryId = categoryId;
                getCategoryById(data).then((res) => {
                    if (res.code !== 200) {
                        return this.$message.error(res.message);
                    }
                    this.editForm = res.data;
                })
            },
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
            },
            changeCategory() {
                this.$refs.editFormRef.validate(valid => {
                    if (!valid) return;
                    editCategory(this.editForm).then((res) => {
                        if (res.code !== 200) {
                            this.$message.error(res.message);
                        }
                        this.editDialogVisible = false;
                        this.getCategoryList();
                        this.$message.success(res.message);
                    })
                })
            },
            deleteCategory(categoryId) {
                const confirmResult = this.$confirm(
                    '此操作将永久删除该分类, 是否继续?',
                    '提示',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                    let data = {};
                    data.categoryId = categoryId;
                    data.isAlive = 0;
                    editCategory(data).then((res) => {
                        if (res.code !== 200) {
                            return this.$message.error("删除分类失败！");
                        }
                        this.$message.success("删除分类成功！");
                    })
                }).catch(() => {
                    this.$message.info('已取消删除');
                });
                },
            },
            created() {
                this.getCategoryList();
            }
        }
</script>

<style scoped>
  @import "../../styles/css/global.css";

  /deep/ .zk-table {
    margin-top: 15px;
  }

  /deep/ .el-cascader {
    width: 100%;
  }
</style>
